﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL;

namespace MINERP.BLL.InfoConfig
{
    public class ProductCommomChargeItemBLL
    {
       
        /// <summary>
        /// 获取所有的产品计价
        /// </summary>
        /// <returns></returns>
       public  List<ProductCommonChargeItem_Info> GetAllProductCommonItem()
        {
            List<ProductCommonChargeItem_Info> rtnList = new List<ProductCommonChargeItem_Info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                
                List<Tb_Product> listProduct = db.Tb_Product.Where(p=>p.Status==1).ToList();
                List<CommonChargeItem> listCommonItem = db.CommonChargeItem.ToList();
                foreach (Tb_Product product in listProduct)
                {
                    ProductCommonChargeItem_Info info = new ProductCommonChargeItem_Info();
                    product.Tb_PorductCategoryReference.Load();
                    info.ProductId = product.ProuctId;
                    info.ProductName = product.ProductName;
                    info.ProductCategory = product.Tb_PorductCategory.ProductCategoryName;
                    info.Size = product.Width + "x" + product.Height;
                    product.CommonChargeItem.Load();
                    
                    info.CommonChargeItem = new List<CommonChargeItemInfo>();

                    //if (product.CommonChargeItem.Count > 0)
                    //{
                    //    foreach (CommonChargeItem cci in product.CommonChargeItem)
                    //    {

                    //        CommonChargeItemInfo cinfo = new CommonChargeItemInfo();
                    //        cinfo.CommonChargeItemID = cci.CommonChargeItemId;
                    //        cinfo.CommonChargeItemName = cci.ItemName;
                    //        cinfo.isProductCommom = true;
                    //        cinfo.isProductCommom2 = false;
                    //        info.CommonChargeItem.Add(cinfo);
                    //    }


                    //}
                    foreach (CommonChargeItem cci in listCommonItem)
                    {
                        CommonChargeItemInfo cinfo = new CommonChargeItemInfo();
                        cinfo.CommonChargeItemID = cci.CommonChargeItemId;
                        cinfo.CommonChargeItemName = cci.ItemName;
                        if (product.CommonChargeItem.FirstOrDefault(p => p.CommonChargeItemId == cci.CommonChargeItemId) != null)
                        {
                            cinfo.isProductCommom = true;
                        }
                        else
                        {
                            cinfo.isProductCommom = false;
                        }
                        cinfo.isProductCommom2 = false;
                        info.CommonChargeItem.Add(cinfo);

                    }
                    rtnList.Add(info);
                }
 
            }
            return rtnList;
        }
        /// <summary>
        /// 保存更改
        /// </summary>
        /// <param name="inProduct"></param>
        public void   UpdataProductCommonItem( ProductCommonChargeItem_Info inProduct)
        {
            
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                Tb_Product product = db.Tb_Product.FirstOrDefault(p => p.ProuctId == inProduct.ProductId);
                product.CommonChargeItem.Load();
                List<CommonChargeItem> listCommonItem = db.CommonChargeItem.ToList();
                bool isNeedSave=false;
                foreach (CommonChargeItem cci in listCommonItem)
                {
                    if (inProduct.CommonChargeItem.FirstOrDefault(p => p.CommonChargeItemID == cci.CommonChargeItemId && p.isProductCommom) != null)
                    {
                        if (product.CommonChargeItem.FirstOrDefault(p => p.CommonChargeItemId == cci.CommonChargeItemId) != null)
                        {
                            ///旧项目不做更改
                        }
                        else
                        {
                            ///新添加项目
                            product.CommonChargeItem.Add(cci);
                            isNeedSave = true;
                        }
                    }
                    else
                    {
                        if (product.CommonChargeItem.FirstOrDefault(p => p.CommonChargeItemId == cci.CommonChargeItemId) != null)
                        {
                            ///旧项目要删除的旧项目
                            product.CommonChargeItem.Remove(cci);
                            isNeedSave = true;
                        }
                        else
                        {
                            ///无操作
                        }
                    }
                }
                if (isNeedSave)
                {
                    db.SaveChanges();
                }

            }
               
 
        }
    }
    

    public class ProductCommonChargeItem_Info
    {
        public int ProductId { get; set; }
        public List<CommonChargeItemInfo> CommonChargeItem { get; set; }
        public string ProductName { get; set; }
        public string ProductCategory { get; set; }
        public string Size { get; set; }
       
    }
    public class CommonChargeItemInfo
    {
        public int CommonChargeItemID { get; set; }
        public string CommonChargeItemName { get; set; }
        public bool isProductCommom { get; set; }
        public bool isProductCommom2 { get; set; }//更新时对比用
    }
}
